package com.spring.test.dao.impl;

import com.spring.test.dao.UserDao;
import com.spring.test.domain.User;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<User> findAll() {
        List<User> userList = jdbcTemplate.query("select * from sys_user", new BeanPropertyRowMapper<User>(User.class));
        return userList;
    }

    @Override
    public Long save(User user) {

        //返回存入数据的id

        //创建PreparedStatementCreator
        PreparedStatementCreator pre = new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                //使用原始jdbc完成这个PreparedStatement的组件
                PreparedStatement statement = connection.prepareStatement("insert into sys_user values (?,?,?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS);
                statement.setObject(1, null);
                statement.setObject(2, user.getUsername());
                statement.setObject(3, user.getEmail());
                statement.setObject(4, user.getPassword());
                statement.setObject(5, user.getPhoneNum());
                return statement;
            }
        };

        //创建keyHolder
        GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();

        jdbcTemplate.update(pre, keyHolder);

        //获得生成的主建
        long userid = keyHolder.getKey().longValue();

        return userid;

        //jdbcTemplate.update("insert into sys_user values (?,?,?,?,?)", null, user.getUsername(), user.getEmail(), user.getPassword(), user.getPhoneNum());

    }

    @Override
    public void saveUserRoleRel(Long id, Long[] roleIds) {

        for (Long roleId : roleIds) {
            jdbcTemplate.update("insert into sys_user_role values (?,?) ", id, roleId);
        }

    }

    @Override
    public void delUserRoleRel(Long userId) {
        jdbcTemplate.update("delete from sys_user_role where userId = ?", userId);
    }

    @Override
    public void del(Long userId) {

        jdbcTemplate.update("delete from sys_user where id = ?", userId);

    }

    @Override
    public User findByUsernameAndPassword(String username, String password) throws EmptyResultDataAccessException {

        User user = jdbcTemplate.queryForObject(" select * from sys_user where username = ? and password = ? ", new BeanPropertyRowMapper<User>(User.class), username, password);

        return user;

    }

}
